<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>模板应用</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      margin: 0;
      padding: 0;
      background-color: #f7f7f7;
      color: #333;
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      padding: 30px;
      background-color: #fff;
      box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      border-bottom: 2px solid #ddd;
      padding-bottom: 15px;
    }

    .header h1 {
      margin: 0;
      font-size: 2.5em;
      color: #007bff;
      font-weight: bold;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group label {
      display: block;
      margin-bottom: 8px;
      font-size: 1.1em;
      color: #555;
    }

    .form-group input, .form-group button {
      width: 100%;
      padding: 12px;
      box-sizing: border-box;
      border: 1px solid #ddd;
      border-radius: 5px;
      font-size: 1em;
    }

    .form-group input:focus, .form-group button:focus {
      border-color: #007bff;
      outline: none;
    }

    button {
      padding: 10px 20px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      font-size: 1em;
      transition: background-color 0.3s;
    }

    button:hover {
      background-color: #0056b3;
    }

    .image-list {
      display: flex;
      flex-wrap: wrap;
      padding: 0;
      margin-bottom: 30px;
      list-style: none;
    }

    .image-list li {
      margin-right: 15px;
      margin-bottom: 15px;
      border: 2px solid transparent;
      transition: border-color 0.3s;
      cursor: pointer;
      width: calc(20% - 15px); /* 每行显示5个，减去右边距 */
    }

    .image-list li.selected {
      border-color: #007bff;
    }

    .image-list img {
      width: 100%;
      height: auto;
      object-fit: cover;
      border-radius: 5px;
    }

    .alert {
      padding: 10px;
      margin-bottom: 15px;
      border: 1px solid transparent;
      border-radius: 4px;
    }

    .alert-success {
      color: #3c763d;
      background-color: #dff0d8;
      border-color: #d6e9c6;
    }

    .alert-danger {
      color: #a94442;
      background-color: #f2dede;
      border-color: #ebccd1;
    }
  </style>
</head>
<body>
<div class="container">
  <div class="header">
    <h1>模板应用</h1>
  </div>

  <!-- 图片列表 -->
  <ul id="image-list-items" class="image-list"></ul>

  <!-- 选择覆盖图片 -->
  <div class="form-group">
    <label for="overlayImage">选择覆盖图片</label>
    <input type="file" id="overlayImage" accept="image/*">
  </div>
  <button onclick="applyTemplate()">应用模板</button>
  <div id="message" class="alert"></div>

  <!-- 生成的 GIF -->
  <h2>生成的 GIF</h2>
  <img id="generatedGif" src="" alt="生成的 GIF" style="display: none;">
</div>

<script>
    const apiBaseUrl = '/api/web';
    let selectedTemplateId = '';

    // 获取模板列表
    function fetchTemplates() {
        const imageListItems = document.getElementById('image-list-items');

        fetch(`${apiBaseUrl}/list`, {
            method: 'POST'
        })
        .then(response => response.json())
        .then(data => {
            imageListItems.innerHTML = '';
            data.forEach(template => {
                const li = document.createElement('li');
                const img = document.createElement('img');
                img.src = `data:image/gif;base64,${template.gifBase64}`;
                li.appendChild(img);
                li.addEventListener('click', () => {
                    selectedTemplateId = template.templateId;
                    imageListItems.querySelectorAll('li').forEach(item => item.classList.remove('selected'));
                    li.classList.add('selected');
                });
                imageListItems.appendChild(li);
            });
        })
        .catch(error => {
            alert(error.message);
        });
    }

    // 应用模板
    function applyTemplate() {
        const overlayImageInput = document.getElementById('overlayImage');
        const overlayImage = overlayImageInput.files[0];
        const messageDiv = document.getElementById('message');
        const generatedGif = document.getElementById('generatedGif');

        if (!selectedTemplateId || !overlayImage) {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = '请选择一个模板和一个覆盖图片';
            return;
        }

        const formData = new FormData();
        formData.append('templateId', selectedTemplateId);
        formData.append('overlayImage', overlayImage);

        fetch(`${apiBaseUrl}/create`, {
            method: 'POST',
           body: formData
})
.then(response => response.text())
.then(data => {
messageDiv.className = 'alert alert-success';
messageDiv.textContent = '模板应用成功';
generatedGif.src = data;
generatedGif.style.display = 'block';
})
.catch(error => {
messageDiv.className = 'alert alert-danger';
messageDiv.textContent = error.message;
});
}
  // 初始化
fetchTemplates();</script> </body> </html>